package StudentAdminister;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudenTest {
    public static void main(String[] args) {
        ArrayList<Student> list1 = new ArrayList();
        Scanner scanner = new Scanner(System.in); // 将Scanner移到循环外，避免资源浪费

        while (true) { // 无限循环替代固定次数循环
            System.out.println("--------欢迎使用清华大学宜宾学院学生管理系统--------");
            System.out.println("1--添加学生");
            System.out.println("2--删除学生");
            System.out.println("3--修改学生");
            System.out.println("4--查看所有学生信息");
            System.out.println("5--查看指订学生信息");
            System.out.println("6--退出系统");
            System.out.println("请键盘录入你的选择：");

            int chosse = scanner.nextInt();
            scanner.nextLine();

            if (chosse == 6) {
                System.out.println("您已退出清华大学宜宾学院学生管理系统，再见！");
                return;
            }
            chosseOpreate(chosse, list1);

            System.out.println("______________________________________");
        }
    }

    public static void chosseOpreate(int chosse, List<Student> list) {
        switch (chosse) {
            case 1://添加学生
                addStudent(list);
                break;
            case 2://删除学生
                deleteStudent(list);
                break;
            case 3://修改学生
                setStudent(list);
                break;
            case 4://查看所有学生
                searchStudent(list);
                break;
            case 5://查看指定学生信息
                searchSingleStudent(list);
        }
    }
//1.添加学生信息
    public static void addStudent(List<Student> list) {
        Scanner scanner = new Scanner(System.in);

        while (true) { // 添加循环处理重复ID
            System.out.println("请输入学生学号:");
            int id = scanner.nextInt();
            scanner.nextLine();

            boolean flag = judgeId(id, list);
            if (flag) {
                System.out.println("这个学号已经存在了,请重新输入!");
            } else {
                System.out.println("请输入学生姓名:");
                String name = scanner.nextLine();

                System.out.println("请输入学生年龄:");
                int age = scanner.nextInt();
                scanner.nextLine();

                System.out.println("请输入学生居住地:");
                String adress = scanner.nextLine();

                System.out.println("新学生添加成功！");
                list.add(new Student(id, name, age, adress));
                break;
            }
        }
    }

    //4.输出全部学生信息
    public static void searchStudent(List<Student> list) {
        System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
        System.out.println("______________________________________");
        for (Student student : list) {
            System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "岁\t\t" + student.getAdress());
        }
    }

    //查询学生学号是否重重复
    public static boolean judgeId(int id, List<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            //重复的id
            if (list.get(i).getId() == id) {
                return true;
            }
        }
        //不重复
        return false;
    }
//2.删除学生信息
    public static void deleteStudent(List<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您想要尝试删除的学号：");
        int id = scanner.nextInt();
        scanner.nextLine();

        // 查找学生索引
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == id) {
                index = i;
                break;
            }
        }

        if (index != -1) {
            list.remove(index);
            System.out.println("删除成功");
        } else {
            System.out.println("该学生id不存在！请重新输入");
        }
    }
//3.修改学生信息
    public static void setStudent(List<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您想要修改的学生号：");
        int id = scanner.nextInt();
        scanner.nextLine();

        // 查找学生索引
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == id) {
                index = i;
                break;
            }
        }

        if (index != -1) {
            System.out.println("请输入学生新姓名：");
            String name = scanner.nextLine();

            System.out.println("请输入学生新年龄：");
            int age = scanner.nextInt();
            scanner.nextLine();

            System.out.println("请输入学生新居住地：");
            String adress = scanner.nextLine();

            // 更新学生信息
            Student student = list.get(index);
            student.setName(name);
            student.setAge(age);
            student.setAdress(adress);

            System.out.println("学生信息修改成功：");
        } else {
            System.out.println("该学生id不存在！");
        }
    }
    public static void searchSingleStudent(List<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您想要查找的学号：");
        int id = scanner.nextInt();
        scanner.nextLine();

        // 查找学生索引
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == id) {
                index = i;
                break;
            }
        }

        if (index != -1) {//该学生学号存在
            System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
            System.out.println("__________________________________________________");
            Student student = list.get(index);
                System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "岁\t\t" + student.getAdress());
        } else {
            System.out.println("该学生id不存在！请重新输入");
        }
    }
}